package com.student.education.controller;

import com.student.education.common.CommonResult;
import com.student.education.entity.Term;
import com.student.education.service.TermService;
import com.student.education.to.TermTo;
import com.student.education.vo.TermVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学期班级表(Term)表控制层
 *
 * @author makejava
 * @since 2021-10-08 22:59:53
 */
@RestController
@RequestMapping("term")
public class TermController {
    /**
     * 服务对象
     */
    @Resource
    private TermService termService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Term> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.termService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param term 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody TermVo term) {
        int i = termService.insert(term);
        if (i>0){
            return CommonResult.success("新增成功");
        }
        return CommonResult.error("新增失败");
    }

    /**
     * 新增数据
     *
     * @param termName 实体
     * @return 新增结果
     */
    @GetMapping("/queryAll")
    public CommonResult<List<TermTo>> queryAll(String termName) {
        return CommonResult.success(termService.queryAll(termName));
    }
    /**
     * 编辑数据
     *
     * @param term 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody TermVo term) {
        int result = termService.update(term);
        if (result>0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public CommonResult<String> deleteById(Integer id) {
        if (termService.deleteById(id)){
            return CommonResult.success("删除成功");
        }
        return CommonResult.success("删除失败");
    }

}

